#include "Window.h"

Window::Window()
{
    //设置窗口
    screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE | SDL_RESIZABLE );
    
    //如果发生错误
    if( screen == NULL )
    {
        windowStatus = WINDOW_SCREENERR;
    }
    else
    {
        windowStatus = WINDOW_OK;    
    }
    
    screenColor.r = 255;
    screenColor.g = 255;
    screenColor.b = 255;

    //设置窗口标题
    SDL_WM_SetCaption( "Window Event Test", NULL );
    
    //设置窗口状态标志
    windowed = true;
}


void Window::toggle_fullscreen()
{
    //如果窗口是窗口化的
    if( windowed == true )
    {
        //设置窗口为全屏模式
        screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE | SDL_RESIZABLE | SDL_FULLSCREEN );
                         
        //如果发生了错误
        if( screen == NULL )
        {
            windowStatus = WINDOW_SCREENERR;
            return;
        }
        
        //设置窗口状态标志
        windowed = false;
    }
    //如果窗口是全屏的
    else if( windowed == false )
    {
        //将窗口窗口化
        screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE | SDL_RESIZABLE );
                     
        //如果发生了错误
        if( screen == NULL )
        {
            windowStatus = WINDOW_SCREENERR;
            return;
        }
       
        //设置窗口状态标志
        windowed = true;
    }
}

void Window::handle_events(SDL_Event event)
{
    //如果窗口有错误
    if( windowStatus != WINDOW_OK )
    {
        return;    
    }
    
    //如果窗口被缩放
    if( event.type == SDL_VIDEORESIZE )
    {
        //重新设定窗口大小
        screen = SDL_SetVideoMode( event.resize.w, event.resize.h, SCREEN_BPP, SDL_SWSURFACE | SDL_RESIZABLE );
        
        //如果出现错误
        if( screen == NULL )
        {
            windowStatus = WINDOW_SCREENERR;
            return;
        }
    }
    //如果“F11”键被按下
    else if( ( event.type == SDL_KEYDOWN ) && ( event.key.keysym.sym == SDLK_F11 ) )
    {
        //切换全屏/窗口
        toggle_fullscreen();
    }
    //如果窗口焦点发生变化
    else if( event.type == SDL_ACTIVEEVENT )
    {
        //如果窗口被最小化或还原
        if( event.active.state & SDL_APPACTIVE )
        {
            //如果程序不再是激活状态
            if( event.active.gain == 0 )
            {
                SDL_WM_SetCaption( "Window Event Test: Iconified", NULL );
            }
            else
            {
                SDL_WM_SetCaption( "Window Event Test", NULL );
            }
        }
        //如果键盘焦点产生了事件
        else if( event.active.state & SDL_APPINPUTFOCUS )
        {
            //如果程序失去了键盘焦点
            if( event.active.gain == 0 )
            {
                SDL_WM_SetCaption( "Window Event Test: Keyboard focus lost", NULL );
            }
            else
            {
                SDL_WM_SetCaption( "Window Event Test", NULL );
            }
        }
        //如果鼠标焦点产生了事件
        else if( event.active.state & SDL_APPMOUSEFOCUS )
        {
            //如果程序失去了鼠标焦点
            if( event.active.gain == 0 )
            {
                SDL_WM_SetCaption( "Window Event Test: Mouse Focus Lost", NULL );
            }
            else
            {
                SDL_WM_SetCaption( "Window Event Test", NULL );
            }
        }
    }
    //如果窗口被修改
    else if( event.type == SDL_VIDEOEXPOSE )
    {
        updateDraw();
    }
}

int Window::getWindowStatus()
{
    return windowStatus;
}

bool Window::updateDraw()
{
    //更新窗口
    if( SDL_Flip( screen ) == -1 )
    {
        cout << "SDL_Flip" << strerror(errno) << endl;
        windowStatus = WINDOW_DRAWERR;
        return false;    
    }

    return true;
}

void Window::clearScreen()
{
    //用白色填充窗口
    SDL_FillRect( screen, &screen->clip_rect, \
        SDL_MapRGB( screen->format, screenColor.r, screenColor.g, screenColor.b ) );

}